package com.lwbpro.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwbpro.common.exception.ServiceException;
import com.lwbpro.common.utils.DateUtils;
import com.lwbpro.system.domain.AchievementManage;
import com.lwbpro.system.domain.BarGraphVO;
import com.lwbpro.system.domain.StudentManage;
import com.lwbpro.system.domain.vo.BarGraphRespVO;
import com.lwbpro.system.domain.vo.ExamManageExcelVO;
import com.lwbpro.system.domain.vo.ExamManageReqVO;
import com.lwbpro.system.service.IAchievementManageService;
import com.lwbpro.system.service.IStudentManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.lwbpro.system.mapper.ExamManageMapper;
import com.lwbpro.system.domain.ExamManage;
import com.lwbpro.system.service.IExamManageService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 考试管理Service业务层处理
 *
 * @author achievement
 * @date 2024-08-28
 */
@Service
public class ExamManageServiceImpl extends ServiceImpl<ExamManageMapper, ExamManage> implements IExamManageService {
    @Resource
    private ExamManageMapper examManageMapper;

    @Resource
    private IStudentManageService iStudentManageService;

    @Resource
    private IAchievementManageService iAchievementManageService;


    /**
     * 查询考试管理
     *
     * @param id 考试管理主键
     * @return 考试管理
     */
    @Override
    public ExamManage selectExamManageById(Long id) {
        return examManageMapper.selectExamManageById(id);
    }

    /**
     * 查询考试管理列表
     *
     * @param reqVO 考试管理
     * @return 考试管理
     */
    @Override
    public List<ExamManage> selectExamManageList(ExamManageReqVO reqVO) {
        return examManageMapper.selectExamManageList(reqVO);
    }

    /**
     * 新增考试管理
     *
     * @param examManage 考试管理
     * @return 结果
     */
    @Override
    public int insertExamManage(ExamManage examManage) {
        examManage.setCreateTime(DateUtils.getNowDate());
        return examManageMapper.insertExamManage(examManage);
    }

    /**
     * 修改考试管理
     *
     * @param examManage 考试管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateExamManage(ExamManage examManage) {
        ExamManage checkName = examManageMapper.checkName(examManage);
        if (checkName != null) {
            throw new ServiceException(examManage.getName() + "考试名称已存在!");
        }

        iAchievementManageService.updateExamManageByExamId(examManage.getId(), examManage.getName());
        examManage.setUpdateTime(DateUtils.getNowDate());
        return examManageMapper.updateExamManage(examManage);
    }

    /**
     * 批量删除考试管理
     *
     * @param ids 需要删除的考试管理主键
     * @return 结果
     */
    @Override
    public int deleteExamManageByIds(Long[] ids) {
        return examManageMapper.deleteExamManageByIds(ids);
    }

    /**
     * 删除考试管理信息
     *
     * @param id 考试管理主键
     * @return 结果
     */
    @Override
    public int deleteExamManageById(Long id) {
        return examManageMapper.deleteExamManageById(id);
    }


    public Long IdUtil() {
        return IdUtil.getSnowflake(1, 1).nextId();
    }

    /**
     * 柱状图
     *
     * @param id
     * @return
     */

    @Override
    public BarGraphRespVO barGraph(Long id) {
        BarGraphVO barGraphVO = examManageMapper.barGraph(id);
        BarGraphRespVO graphRespVO = new BarGraphRespVO();
        if (barGraphVO == null) {
            return graphRespVO;
        }
        List<Integer> AList = new ArrayList<>();
        List<Integer> BList = new ArrayList<>();
        List<Integer> CList = new ArrayList<>();
        List<Integer> DList = new ArrayList<>();


        AList.add(barGraphVO.getA1());
        AList.add(barGraphVO.getA2());
        AList.add(barGraphVO.getA3());
        AList.add(barGraphVO.getA4());
        AList.add(barGraphVO.getA5());
        AList.add(barGraphVO.getA6());
        AList.add(barGraphVO.getA7());

        BList.add(barGraphVO.getB1());
        BList.add(barGraphVO.getB2());
        BList.add(barGraphVO.getB3());
        BList.add(barGraphVO.getB4());
        BList.add(barGraphVO.getB5());
        BList.add(barGraphVO.getB6());
        BList.add(barGraphVO.getB7());

        CList.add(barGraphVO.getC1());
        CList.add(barGraphVO.getC2());
        CList.add(barGraphVO.getC3());
        CList.add(barGraphVO.getC4());
        CList.add(barGraphVO.getC5());
        CList.add(barGraphVO.getC6());
        CList.add(barGraphVO.getC7());

        DList.add(barGraphVO.getD1());
        DList.add(barGraphVO.getD2());
        DList.add(barGraphVO.getD3());
        DList.add(barGraphVO.getD4());
        DList.add(barGraphVO.getD5());
        DList.add(barGraphVO.getD6());
        DList.add(barGraphVO.getD7());

        graphRespVO.setExcellentList(AList);
        graphRespVO.setGoodList(BList);
        graphRespVO.setPassList(CList);
        graphRespVO.setFailList(DList);
        return graphRespVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importCase(List<ExamManageExcelVO> examManageList, boolean updateSupport, String operName, String filename) {

        if (CollUtil.isEmpty(examManageList)) {
            return "导入失败，没有需要导入的数据！";
        }


        filename = filename.substring(0, filename.lastIndexOf("."));

        if (StringUtils.isBlank(filename)) {
            throw new ServiceException("考试名称不能为空 请检查!");
        }
        if (filename.length() > 30) {
            throw new ServiceException("考试名称不能超过30个字符 请检查!");
        }

        //重复文件名校验
        ExamManage manage = new ExamManage();
        manage.setName(filename);
        ExamManage checkName = examManageMapper.checkName(manage);
        if (checkName != null) {
            throw new ServiceException(filename + "考试名称已存在!");
        }


        ExamManage examManage = new ExamManage();
        examManage.setId(IdUtil());
        examManage.setName(filename);
        examManage.setCreateBy(operName);
        examManage.setCreateTime(DateUtils.getNowDate());
        examManage.setUpdateTime(DateUtils.getNowDate());
        examManage.setUpdateBy(operName);
        examManage.setRemark(filename);


        //根据导入的学生信息获取
        List<String> studentNames = examManageList.stream().map(ExamManageExcelVO::getStudentName).collect(Collectors.toList());
        List<StudentManage> studentManageList = iStudentManageService.selectByNames(studentNames);
        List<StudentManage> studentList = new ArrayList<>();
        List<AchievementManage> achievementList = new ArrayList<>();

        examManageList.forEach(item -> {
            StudentManage studentManage = studentManageList.stream().filter(student -> student.getStudentName().equals(item.getStudentName())).findFirst().orElse(null);
            if (studentManage == null) {
                studentManage = new StudentManage();
                studentManage.setId(IdUtil());
                studentManage.setStudentName(item.getStudentName());
                studentManage.setCreateBy(operName);
                studentManage.setCreateTime(DateUtils.getNowDate());
                studentManage.setUpdateTime(DateUtils.getNowDate());
                studentManage.setUpdateBy(operName);
                //数据库不存在则添加
                studentList.add(studentManage);
            }
            //考试成绩记录
            AchievementManage achievementManage = new AchievementManage();
            achievementManage.setId(IdUtil());
            achievementManage.setExamId(examManage.getId());
            achievementManage.setExamName(examManage.getName());
            achievementManage.setStudentId(studentManage.getId());
            achievementManage.setStudentName(studentManage.getStudentName());
            achievementManage.setSubjectOne(item.getSubjectOne());
            achievementManage.setSubjectOneEvaluate(item.getSubjectOneEvaluate());
            achievementManage.setSubjectOneSpeed(item.getSubjectOneSpeed());
            achievementManage.setSubjectTwo(item.getSubjectTwo());
            achievementManage.setSubjectTwoEvaluate(item.getSubjectTwoEvaluate());
            achievementManage.setSubjectTwoSpeed(item.getSubjectTwoSpeed());
            achievementManage.setSubjectThree(item.getSubjectThree());
            achievementManage.setSubjectThreeEvaluate(item.getSubjectThreeEvaluate());
            achievementManage.setSubjectThreeSpeed(item.getSubjectThreeSpeed());
            achievementManage.setSubjectFour(item.getSubjectFour());
            achievementManage.setSubjectFourEvaluate(item.getSubjectFourEvaluate());
            achievementManage.setSubjectFourSpeed(item.getSubjectFourSpeed());
            achievementManage.setSubjectFive(item.getSubjectFive());
            achievementManage.setSubjectFiveEvaluate(item.getSubjectFiveEvaluate());
            achievementManage.setSubjectFiveSpeed(item.getSubjectFiveSpeed());
            achievementManage.setSubjectSix(item.getSubjectSix());
            achievementManage.setSubjectSixEvaluate(item.getSubjectSixEvaluate());
            achievementManage.setSubjectSixSpeed(item.getSubjectSixSpeed());
            achievementManage.setSubjectSeven(item.getSubjectSeven());
            achievementManage.setSubjectSevenEvaluate(item.getSubjectSevenEvaluate());
            achievementManage.setSubjectSevenSpeed(item.getSubjectSevenSpeed());
            achievementManage.setCreateBy(operName);
            achievementManage.setCreateTime(DateUtils.getNowDate());
            achievementManage.setUpdateTime(DateUtils.getNowDate());
            achievementManage.setUpdateBy(operName);

            achievementList.add(achievementManage);

        });

        //插入考试管理
        examManageMapper.insert(examManage);
        //保存学生信息
        iStudentManageService.saveBatch(studentList);
        //保存考试成绩记录
        iAchievementManageService.saveBatch(achievementList);

        return examManage.getName() + "导入成功";
    }
}
